//-----------------------------------------------------------------------
// <copyright file="a_StatsByMonthDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_StatsByMonthDAOBase
    {
        #region Common methods
        public virtual a_StatsByMonth Createa_StatsByMonthFromReader(IDataReader reader)
        {
            a_StatsByMonth item = new a_StatsByMonth();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatDate"))) item.StatDate = (DateTime)reader["StatDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("DocType"))) item.DocType = (int)reader["DocType"];
                if (!reader.IsDBNull(reader.GetOrdinal("DocID"))) item.DocID = (int)reader["DocID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (int)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("Printed"))) item.Printed = (int)reader["Printed"];
                if (!reader.IsDBNull(reader.GetOrdinal("Rated"))) item.Rated = (int)reader["Rated"];
                if (!reader.IsDBNull(reader.GetOrdinal("RatingAvg"))) item.RatingAvg = (int)reader["RatingAvg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Emailed"))) item.Emailed = (int)reader["Emailed"];
                if (!reader.IsDBNull(reader.GetOrdinal("Downloaded"))) item.Downloaded = (int)reader["Downloaded"];
                if (!reader.IsDBNull(reader.GetOrdinal("SavedLike"))) item.SavedLike = (int)reader["SavedLike"];
                if (!reader.IsDBNull(reader.GetOrdinal("SavedBuy"))) item.SavedBuy = (int)reader["SavedBuy"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_StatsByMonth methods
            
        public virtual void Createa_StatsByMonth(a_StatsByMonth a_StatsByMonth)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByMonth_ins");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_StatsByMonth.ID);
                database.AddInParameter(dbCommand, "@StatDate", DbType.DateTime, a_StatsByMonth.StatDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Int32, a_StatsByMonth.DocType);
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, a_StatsByMonth.DocID);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int32, a_StatsByMonth.Viewed);
                database.AddInParameter(dbCommand, "@Printed", DbType.Int32, a_StatsByMonth.Printed);
                database.AddInParameter(dbCommand, "@Rated", DbType.Int32, a_StatsByMonth.Rated);
                database.AddInParameter(dbCommand, "@RatingAvg", DbType.Int32, a_StatsByMonth.RatingAvg);
                database.AddInParameter(dbCommand, "@Emailed", DbType.Int32, a_StatsByMonth.Emailed);
                database.AddInParameter(dbCommand, "@Downloaded", DbType.Int32, a_StatsByMonth.Downloaded);
                database.AddInParameter(dbCommand, "@SavedLike", DbType.Int32, a_StatsByMonth.SavedLike);
                database.AddInParameter(dbCommand, "@SavedBuy", DbType.Int32, a_StatsByMonth.SavedBuy);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_StatsByMonth methods
        
        public virtual void Updatea_StatsByMonth(a_StatsByMonth a_StatsByMonth)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByMonth_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_StatsByMonth.ID);
                database.AddInParameter(dbCommand, "@StatDate", DbType.DateTime, a_StatsByMonth.StatDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Int32, a_StatsByMonth.DocType);
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, a_StatsByMonth.DocID);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int32, a_StatsByMonth.Viewed);
                database.AddInParameter(dbCommand, "@Printed", DbType.Int32, a_StatsByMonth.Printed);
                database.AddInParameter(dbCommand, "@Rated", DbType.Int32, a_StatsByMonth.Rated);
                database.AddInParameter(dbCommand, "@RatingAvg", DbType.Int32, a_StatsByMonth.RatingAvg);
                database.AddInParameter(dbCommand, "@Emailed", DbType.Int32, a_StatsByMonth.Emailed);
                database.AddInParameter(dbCommand, "@Downloaded", DbType.Int32, a_StatsByMonth.Downloaded);
                database.AddInParameter(dbCommand, "@SavedLike", DbType.Int32, a_StatsByMonth.SavedLike);
                database.AddInParameter(dbCommand, "@SavedBuy", DbType.Int32, a_StatsByMonth.SavedBuy);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_StatsByMonth methods
        public virtual void Deletea_StatsByMonth(int docID, DateTime statDate, int docType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByMonth_del");
                
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, docID);
                database.AddInParameter(dbCommand, "@StatDate", DbType.DateTime, statDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Int32, docType);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_StatsByMonth methods
        
        public virtual a_StatsByMonth Geta_StatsByMonth(int docID, DateTime statDate, int docType)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByMonth_sel");
                
                database.AddInParameter(dbCommand, "@DocID", DbType.Int32, docID);
                database.AddInParameter(dbCommand, "@StatDate", DbType.DateTime, statDate);
                database.AddInParameter(dbCommand, "@DocType", DbType.Int32, docType);
                
                a_StatsByMonth a_StatsByMonth =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_StatsByMonth = Createa_StatsByMonthFromReader(reader);
                        reader.Close();
                    }
                }
                return a_StatsByMonth;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_StatsByMonthList methods
        public virtual a_StatsByMonthCollection Geta_StatsByMonthList(a_StatsByMonthColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_StatsByMonth_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_StatsByMonthCollection a_StatsByMonthCollection = new a_StatsByMonthCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_StatsByMonth a_StatsByMonth = Createa_StatsByMonthFromReader(reader);
                        a_StatsByMonthCollection.Add(a_StatsByMonth);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_StatsByMonthCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_StatsByMonthCollection Geta_StatsByMonthList(a_StatsByMonthColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_StatsByMonthList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}